package com.diyunapp.xilinapp.utils;

import org.simple.eventbus.EventBus;

import java.util.Date;

/**
 * Created by Administrator on 2016/3/24 0024.
 */
public class CountDown extends Thread{

    private long currentTime;

    private long distTime;

    private long day, hour, minutes, seconds;

    private long timestamp ;

    private boolean isPause = false;

    private boolean isCancel = false;

    public CountDown(Long timestamp) {
        this.timestamp = timestamp;
    }

    @Override
    public void run() {
        super.run();
        while (true){
            //是否继续
            if(isCancel){
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {

            }
            if(!isPause){
                //设置预定的时间
                currentTime = new Date().getTime();
                //距离的时间
                distTime = timestamp - currentTime;
                //得到天数
                day = ((distTime / 1000) / (3600 * 24));
                //得到小时数
                hour = ((distTime / 1000) - day * 86400) / 3600;
                //得到分钟数
                minutes = ((distTime / 1000) - day * 86400 - hour * 3600) / 60;
                //得到秒数
                seconds = (distTime / 1000) - day * 86400 - hour * 3600 - minutes * 60;
                //更新数据
                EventBus.getDefault().post(day + "天" + hour + "时" + minutes+ "分" + seconds + "秒","index.bidinfo");
            }
        }
    }

    public void pause(){
        isPause = true;
    }

    public void restart(){
        isPause = false;
    }

    public void remove(){
        isCancel = true;
    }
}
